class Simulation

Class that contains the overall simulation. Manages the scape and the bands.

Constants

GRAPH_CACHE_SIZE

Attributes

epoch[R]
graph_caching_enabled[R]
logging_enabled[R]
scape[R]

Public Class Methods

new(config_file) click to toggle source

Initializes a new simulation. The config file is parsed and parameters are passed on to the other objects that make up the simulation.

# File lib/simulation.rb, line 26
def initialize(config_file)
  supposed_config = generate_config(config_file)
  begin
    @config = YAML.load(supposed_config)
  rescue CustomErrors::InvalidConfigError
    warn "Error: invalid config; dumping to invalid.cfg"
    File.open("invalid.cfg") { |f| f.write(supposed_config) }
    raise "Invalid config"
  end
  reset
end

Public Instance Methods

create_population_graph() click to toggle source

Creates a graph of the current population and saves it with a timestamped filename to the “graphs” folder. The filepath is returned.

# File lib/simulation.rb, line 151
def create_population_graph
  graph = Gruff::Line.new
  graph.title = "Population"
  graph.y_axis_label = "Population"
  graph.x_axis_label = "Epochs"
  graph.labels = create_graph_labels
  graph.data(:Population, @population_cache)
  graph.data(:Deaths, @num_deaths_cache)
  write_graph_file(graph, "population")
end
create_resources_graph() click to toggle source

Creates a graph of the current resource consumption and saves it with a timestamped filename to the “graphs” folder. The filepath is returned.

# File lib/simulation.rb, line 165
def create_resources_graph
  graph = Gruff::Line.new
  graph.title = "Resource consumption"
  graph.y_axis_label = "Calories consumed"
  graph.x_axis_label = "Epochs"
  graph.labels = create_graph_labels
  data = {}
  @resource_consumption_cache.each do |entry|
    entry.each do |k, v|
      data[k] = data.fetch(k, []) << v
    end
  end
  data.each { |k, v| graph.data(k, v) }
  graph.marker_font_size = 16
  write_graph_file(graph, "resources")
end
do_time_step() click to toggle source

Performs one epoch (year) of the simulation. Subsequent calls of 'do_time_step' are triggered on the scape and the bands. Logging and graph statistics caching also happen here if they have been started.

# File lib/simulation.rb, line 51
def do_time_step
  log if @logging_enabled
  cache_graph_data if @graph_caching_enabled

  @epoch += 1
  @scape.do_time_step
  new_bands = []
  @bands.each do |b|
    new_bands += b.do_time_step
  end
  @bands = new_bands
end
reset() click to toggle source

Resets the simulation to its start state

# File lib/simulation.rb, line 40
def reset
  @scape, @bands = parse_config(@config)
  @epoch = 0
  @logging_enabled = false
  @graph_caching_enabled = false
end
start_graph_caching() click to toggle source

Initializes and enables graph statistics caching. This is necessary if graphs are desired. A folder called “graphs” will be created if one does not already exist. Graphs will be stored there.

# File lib/simulation.rb, line 135
def start_graph_caching
  return if @graph_caching_enabled

  Dir.mkdir("graphs") unless File.exists?("graphs")

  @population_cache = BoundedQueue.new(GRAPH_CACHE_SIZE) # Integers
  @num_deaths_cache = BoundedQueue.new(GRAPH_CACHE_SIZE) # Integers

  @resource_consumption_cache = BoundedQueue.new(GRAPH_CACHE_SIZE) # string : float maps

  @graph_caching_enabled = true
end
start_logging() click to toggle source

Initialize and enable logging for the bands and the scape. A folder, “logs”, will be created if one does not already exist. Logfiles are timestamped text files.

# File lib/simulation.rb, line 221
def start_logging
  return if @logging_enabled

  Dir.mkdir("logs") unless File.exists?("logs")
  @bands_logfile = open_logfile("bands") if @bands_logfile.nil?
  @scape_logfile = open_logfile("scape") if @scape_logfile.nil?

  @logging_enabled = true
end